<!--
  * 产线机器设备表
  *
  * @Author:    lmabbe
  * @Date:      2025-06-26 19:17:37
  * @Copyright  lmabbe
-->
<template>
  <a-modal :destroyOnClose="true" :maskClosable="false" :open="visibleFlag" :title="form.machineId ? '编辑' : '添加'" :width="450" @cancel="onClose">
    <a-form ref="formRef" :label-col="{ span: 5 }" :model="form" :rules="rules">
      <a-input v-model:value="form.machineId" hidden placeholder="机器ID" style="width: 100%" />
      <a-form-item label="机器名称" name="machineName">
        <a-input v-model:value="form.machineName" placeholder="机器名称" style="width: 100%" />
      </a-form-item>
      <a-form-item label="归属线别" name="belongLineId">
        <a-select
          v-model:value="form.belongLineId"
          :allowClear="true"
          :style="`width: 100%`"
          placeholder="请选择归属线别"
          @change="
            (value) => {
              form.belongLineName = productLineData.find((item) => item.lineId === value).lineName;
            }
          "
        >
          <a-select-option v-for="item in productLineData" :key="item.lineId" :value="item.lineId">
            {{ item.lineName }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-input v-model:value="form.belongLineName" hidden placeholder="归属线别名称" style="width: 100%" />
      <a-form-item label="真实机器名称" name="realMachineName">
        <a-input v-model:value="form.realMachineName" placeholder="真实机器名称" style="width: 100%" />
      </a-form-item>
      <a-form-item label="备注" name="remark">
        <a-textarea v-model:value="form.remark" placeholder="备注" style="width: 100%" />
      </a-form-item>
    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-modal>
</template>
<script setup>
  import { nextTick, onMounted, reactive, ref } from 'vue';
  import _ from 'lodash';
  import { message } from 'ant-design-vue';
  import { SmartLoading } from '/@/components/framework/smart-loading';
  import { productLineInfoApi } from '/@/api/business/product/line/product-line-info-api.js';
  import { machineInfoApi } from '/@/api/business/product/machine/machine-info-api.js';
  import { smartSentry } from '/@/lib/smart-sentry'; // ------------------------ 事件 ------------------------

  // ------------------------ 事件 ------------------------

  const emits = defineEmits(['reloadList']);

  // ------------------------ 显示与隐藏 ------------------------
  // 是否显示
  const visibleFlag = ref(false);

  const productLineData = ref();

  function show(rowData) {
    Object.assign(form, formDefault);
    if (rowData && !_.isEmpty(rowData)) {
      Object.assign(form, rowData);
    }
    // 使用字典时把下面这注释修改成自己的字典字段 有多个字典字段就复制多份同理修改 不然打开表单时不显示字典初始值
    // if (form.status && form.status.length > 0) {
    //   form.status = form.status.map((e) => e.valueCode);
    // }
    visibleFlag.value = true;
    nextTick(() => {
      formRef.value.clearValidate();
    });
  }

  function onClose() {
    Object.assign(form, formDefault);
    visibleFlag.value = false;
  }

  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref();

  const formDefault = {
    machineId: undefined, //机器ID
    machineName: undefined, //机器名称
    belongLineId: undefined, //归属线别
    belongLineName: undefined, //归属线别名称
    realMachineName: undefined, //真实机器名称
    remark: undefined, //备注
  };

  let form = reactive({ ...formDefault });

  const rules = {
    machineName: [{ required: true, message: '机器名称 必填' }],
    belongLineId: [{ required: true, message: '归属线别 必填' }],
  };

  // 点击确定，验证表单
  async function onSubmit() {
    try {
      await formRef.value.validateFields();
      save();
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!');
    }
  }

  // 新建、编辑API
  async function save() {
    SmartLoading.show();
    try {
      if (form.machineId) {
        await machineInfoApi.update(form);
      } else {
        await machineInfoApi.add(form);
      }
      message.success('操作成功');
      emits('reloadList');
      onClose();
    } catch (err) {
      smartSentry.captureError(err);
    } finally {
      SmartLoading.hide();
    }
  }

  function loadProductLine() {
    productLineInfoApi.all().then((res) => {
      productLineData.value = res.data;
    });
  }

  onMounted(() => {
    loadProductLine();
  });
  defineExpose({
    show,
  });
</script>
